﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Globalization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Ko2.Portal;

using System.Diagnostics;

namespace JobShopsDiscussion_v01_6_App18
{

    [Guid("F9638148-4272-4EE6-97E4-AE6CE166D33C")]
    public class JobShopsDiscussion_JobClass_v01_6 : SPJobDefinition
    {

        #region Propertys

        private string _DisplayName =
@"(Ko) Job ShopsDiscussion, ver. 1.0.0.6";
        public override string DisplayName { get{ return _DisplayName; }}

        private string _Description =
@"(Ko) Job ShopsDiscussion, ver. 1.0.0.6";
        public override string Description { get { return _Description; } }

        #endregion Propertys
        #region Members
        public enum UserLocation { UserOffice = 1, UserNotOffice = 2 }; 
        DataTable adUsersOfficeTbl = null;
        DataTable adUsersNotOfficeTbl = null;
        DataTable BirthdaysUsersOfficeTbl = null;
        DataTable BirthdaysUsersNotOfficeTbl = null;
        Ko2.Portal.Env env = new Ko2.Portal.Env();
        string DomainName = new Ko2.Portal.Env().GetAppSetting("DomainName");
        #endregion Members
        #region Common methods
        public string AllTrim(String mStr)
        {
            String vStr = mStr.Replace(" ", string.Empty);
            return vStr;
        }
        public string ConverDate(String mDate)
        {
            String vDate = "";
            //--
            int mCMP = 0;
            if ((mCMP = mDate.IndexOf(".")) != -1)
            {
                vDate = mDate.Substring(6, 4) + "-" + mDate.Substring(3, 2) + "-" + mDate.Substring(0, 2);
            }
            else
            {
                vDate = mDate;
            }
            //--
            return vDate;
        }
        public string InValue(object mValue)
        {
            String mStr;
            if (mValue == null)
                mStr = "";
            else
                mStr = mValue.ToString();
            return mStr;
        }
        #endregion
        #region Constructors
        public JobShopsDiscussion_JobClass_v01_6() : base()
        {
            this.Title = "(Ko) Job ShopsDiscussion";
        }
        public JobShopsDiscussion_JobClass_v01_6(string jobName, SPService service, SPServer server, SPJobLockType targetType) : base(jobName, service, server, targetType)
        {
            this.Title = "(Ko) Job ShopsDiscussion";
        }
        public JobShopsDiscussion_JobClass_v01_6(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "(Ko) Job ShopsDiscussion";
        }
        #endregion Constructors
        #region Job Methods
        public Exception Execute(Guid contentDbId, string siteStr)
        {
            Exception ret = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                Ko2.Portal.Env env = new Ko2.Portal.Env();
                Imap.KoExchWebService o = new Imap.KoExchWebService();
                string uid = env.GetAppSetting("FarmUserUID");
                if (String.IsNullOrEmpty(uid))
                {
                    ret = new Exception("Error: env.GetAppSetting('FarmUserUID')");
                }
                if (ret == null)
                {
                    string[] separator = { @"\" };
                    string[] uid2 = uid.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    if (uid2.Length < 2)
                    {
                        ret = new Exception("Error: env.GetAppSetting('FarmUserUID')");
                    }
                    if (ret == null)
                    {
                        uid = uid2[1];
                        string pwd = env.GetAppSetting("FarmUserPwd");
                        if (String.IsNullOrEmpty(pwd))
                        {
                            ret = new Exception("Error: env.GetAppSetting('FarmUserPwd')");
                        }
                        if (ret == null)
                        {
                            string DomainName = env.GetAppSetting("ProxyDomainName");
                            if (String.IsNullOrEmpty(DomainName))
                            {
                                ret = new Exception("Error: env.GetAppSetting('ProxyDomainName')");
                            }
                            if (ret == null)
                                ret = o.Get_SP_Q_Msg(uid, pwd, DomainName, "SP10_spfarm@korablik.ru");
                        }
                    }
                }

            });

            if (ret != null)
            {
#if DEBUG
                Debug.WriteLine("Error: " + ret.Message);
#endif
            }

            return ret;
        }
        public override void Execute(Guid contentDbId)
        {
            Exception err = Execute(contentDbId, "http://localhost/shops/");
        }
        #endregion

    }

}
